﻿using BCL.BaseLib;
using BCL.BaseLib.Basics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1.Windows
{
    /// <summary>
    /// MainControl.xaml 的交互逻辑
    /// </summary>
    public partial class MainControl : UserControl, ICustom
    {
        public MainControl()
        {
           
        }

        public bool FunctionInvoke(FunctionEntity fe)
        {
            if ((fe.Control.Setting & FunctionControlSetting.UserControl) != 0)
            {
                if (xtraTabControl1.TabPages.Any(o => o.Text == fe.Title))
                {
                    this.xtraTabControl1.SelectedTabPage = xtraTabControl1.TabPages.First(o => o.Text == fe.Title);
                    if (fe.IsReload)
                        fe.Control.ReloadData(fe.ItemParamter);
                }
                else
                {
                    XtraTabPage page = new XtraTabPage();
                    page.Tag = fe;
                    page.Text = fe.Title.Language();
                    fe.Control.Dock = DockStyle.Fill;
                    page.BorderStyle = BorderStyle.None;
                    fe.Control.Function = fe;
                    fe.Control.BorderStyle = BorderStyle.None;
                    page.Controls.Add(fe.Control);
                    xtraTabControl1.TabPages.Add(page);
                    xtraTabControl1.SelectedTabPage = page;
                    tabControlChanged?.Invoke(fe, 1);
                }
            }
            return true;
        }



        public void Select(FunctionEntity fe)
        {
            XtraTabPage page = xtraTabControl1.TabPages.FirstOrDefault(o => o.Tag == fe);
            if (page != null)
                xtraTabControl1.SelectedTabPage = page;
        }

        private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e)
        {
            ClosePageButtonEventArgs cpb = (ClosePageButtonEventArgs)e;
            XtraTabPage xp = (XtraTabPage)cpb.Page;
            this.xtraTabControl1.TabPages.Remove(xp);
            xp.Dispose();
            tabControlChanged?.Invoke(xp.Tag as FunctionEntity, 1);
        }
    }
}
